package com.aisile.crowd_funding.system.controller;

import com.aisile.crowd_funding.common.utils.R;
import com.aisile.crowd_funding.system.pojo.SysRole;
import com.aisile.crowd_funding.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/sys/role")
@Controller
public class RoleController {

   @Autowired
    RoleService roleService;

    @RequestMapping
    public String role(){

        return "/System/role/role";
    }


    @ResponseBody
    @RequestMapping(value = "list")
    public List<SysRole> list(){

        return roleService.selectByExample(null);
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public R delete(Long id){
        int i=roleService.deleteByPrimaryKey(id);
        if(i>0){
            return R.ok();

        }else{
            return R.error("删除失败");
        }

    }

    @RequestMapping(value="add")
    public String add(){

        return "System/role/add";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public R save(SysRole role){
      int i=roleService.insert(role);
      if(i>0){
          return  R.ok();
      }else{
          return R.error("添加失败");
      }

    }
    @RequestMapping("delall")
    @ResponseBody
    public R delall(@RequestParam(value="ids[]" ) Long[] ids){
       int i=roleService.deleteByids(ids);
        if(i==0){
            return  R.error("删除失败");
        }
        return R.ok();
    }

    @RequestMapping("edit/{id}")
    public String toupdate(@PathVariable Long id, Model model){
         SysRole sysRole=roleService.selectByPrimaryKey(id);
         model.addAttribute("role",sysRole);
        return "System/role/update";
    }

    @RequestMapping(value = "doupdate")
    @ResponseBody
    public R update(SysRole role){
          int i= roleService.updateByPrimaryKey(role);
        if(i==0){
            return  R.error("修改失败");
        }
        return R.ok();
    }

}
